var user = JSON.parse(localStorage.getItem('loginUser')).name;
var userKaoqin = {};
var userTel='';
var init = JSON.parse(localStorage.getItem('staff'));
$.each(init, function(index, value) {
	if (user == value.name) {
		$('#userName').text(value.name);
		$('#userDep').text(value.department);
		$('#userPost').text(value.post);
		$('#userBirth').text(value.birthday);
		$('#userImg').attr('src', value.src);
		$('#userIntro').text(value.introduce);
		userKaoqin = value.kaoqin;
		userTel=value.Tel;
		if(value.sex=='女'){
			$('i').removeClass('fa fa-mars').addClass('fa fa-venus').css('background-color','pink');		
			}
		return false;
	}

});
var oDate = new Date().getFullYear();
var day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var leapYearDay = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
$('#yearButton').html(oDate + '<span class="caret"></span>');
for (var i = oDate - 60; i < oDate - 20; i++) {
	$('#yearList').append('<li><a href="javascript:;">' + i + '</a></li>');
}
$('ul').on('click', 'a', function() {
	$(this).parents('ul').prev().html($(this).text() + '<span class="caret"></span>');
	if ($(this).parents('ul').attr('id') == 'yearList') {
		chooseYear = $(this).text();
	}
	if ($(this).parents('ul').attr('id') == 'monthList') {
		chooseMonth = $(this).text();
	}
})
//判断是否为闰年
function isLeapYear(year) {
	parseInt(year);
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {

		return true;
	} else {

		return false;
	}
}
$('#monthButton').on('click', function() {
	$('#dayButton').html(1 + '<span class="caret"></span>');
})
$('#varifyPer').on('click',function(){
	$('input[name="name"]').val($('#userName').text());
	$('input[name="leadingTel"]').val(userTel);
	$('input[name="about"]').val($('#userIntro').text());
	var birthday=$('#userBirth').text();
	var arr=birthday.split('.');
	$('#yearButton').html(arr[0] + '<span class="caret"></span>');
	$('#monthButton').html(arr[1] + '<span class="caret"></span>');
	$('#dayButton').html(arr[2] + '<span class="caret"></span>');
})
$('#dayButton').on('click', function() {
	 $('#dayList').html('');
	if (isLeapYear($('#yearButton').text())) {
		for (var i = 1; i <= leapYearDay[$('#monthButton').text()-1]; i++) {
			$('#dayList').append('<li><a href="javascript:;">' + i + '</a></li>');
		}
	} else {
		for (var i = 1; i <= day[parseInt($('#monthButton').text())-1]; i++) {
			$('#dayList').append('<li><a href="javascript:;">' + i + '</a></li>');
		}

	}

})
//点击头像更改头像
$("#exampleInputFile").change(function() {
	var choose_file = $(this)[0].files[0];
	var reader = new FileReader();
	reader.readAsDataURL(choose_file);
	reader.onload = function() {
		$("#userImg").attr("src", reader.result)
	}
})

$('input').on('input',function(){
	var text=$(this).val();
	var reg=getRegMsg($(this).attr('name'));

	if(reg.test(text)){
		$(this).parent().next().next().css('display','none');
	}else{
		$(this).parent().next().next().css('display','block');
	}
})

$('#savePersonal').on('click',function(){
	var name=$('input[name="name"]').val();
	var birthday=$.trim($('#yearButton').text())+'.'+$.trim($('#monthButton').text())+'.'+$.trim($('#dayButton').text());
	var leadingTel=$('input[name="leadingTel"]').val();
	var about=$('input[name="about"]').val();
	if(getRegMsg('name').test(name)&&getRegMsg('leadingTel').test(leadingTel)){
		var read=JSON.parse(localStorage.getItem('staff'));
		var user=JSON.parse(localStorage.getItem('loginUser'));
		
	      $.each(read, function(index, value) {
			if(value.name==user.name){
				value.name=name;
				value.Tel=leadingTel;
				value.introduce=about;
				value.birthday=birthday;
				user.name=name;
			}
		})
	   localStorage.setItem('staff', JSON.stringify(read));
       localStorage.setItem('loginUser', JSON.stringify(user));
		$('#userName').text(name);
		$('#userIntro').text(about);
		$('#userBirth').text(birthday);
		$('#editUserInfo').modal('hide');
	}else{
	   alert('添加错误或未添加完全！');	
	}
	
})

	function getRegMsg(name) {
			var reg = '';
			switch (name) {
				case 'name':
					reg = nameReg;
					break;
				case 'leading':
					reg = nameReg;
					break;
				case 'leadingTel':
					reg = phoneNumberReg;
					break;
				case 'about':
					reg = depDetailsReg;
					break;
			}
			return reg;
		}
var myChart1 = echarts.init(document.getElementById('echart1'));
option = {
	title: {
		text: '本月考勤'
	},
	tooltip: {
		trigger: 'item',
		formatter: "{a} <br/>{b}: {c} ({d}%)"
	},
	legend: {
		orient: 'vertical',
		x: 'right',
		data: ['准时', '迟到', '缺勤', '请假']
	},
	color: ['#F0AD4E', '#337AB7', '#5CB85C', '#5BC0DE'],
	series: [{
		name: '访问来源',
		type: 'pie',
		radius: ['50%', '70%'],
		avoidLabelOverlap: false,
		label: {
			normal: {
				show: false,
				position: 'center'
			},
			emphasis: {
				show: true,
				textStyle: {
					fontSize: '30',
					fontWeight: 'bold'
				}
			}
		},
		labelLine: {
			normal: {
				show: false
			}
		},
		data: [{
				value: userKaoqin.thisMonthDelay,
				name: '迟到'
			},
			{
				value: userKaoqin.thisMonthPunctual,
				name: '准时'
			},
			{
				value: userKaoqin.thisMonthLeave,
				name: '缺勤'
			},
			{
				value: userKaoqin.thisMonthSick,
				name: '请假'
			},
		]
	}]
};
myChart1.setOption(option);
var myChart2 = echarts.init(document.getElementById('echart2'));
option = {
	title: {
		text: '上月考勤'
	},
	tooltip: {
		trigger: 'item',
		formatter: "{a} <br/>{b}: {c} ({d}%)"
	},
	legend: {
		orient: 'vertical',
		x: 'right',
		data: ['准时', '迟到', '缺勤', '请假']
	},
	color: ['#F0AD4E', '#337AB7', '#5CB85C', '#5BC0DE'],
	series: [{
		name: '访问来源',
		type: 'pie',
		radius: ['50%', '70%'],
		avoidLabelOverlap: false,
		label: {
			normal: {
				show: false,
				position: 'center'
			},
			emphasis: {
				show: true,
				textStyle: {
					fontSize: '30',
					fontWeight: 'bold'
				}
			}
		},
		labelLine: {
			normal: {
				show: false
			}
		},
		data: [{
				value: userKaoqin.lastMonthDelay,
				name: '迟到'
			},
			{
				value: userKaoqin.lastMonthPunctual,
				name: '准时'
			},
			{
				value: userKaoqin.lastMonthLeave,
				name: '缺勤'
			},
			{
				value: userKaoqin.lastMonthSick,
				name: '请假'
			},
		]
	}]
};
myChart2.setOption(option);
var myChart = echarts.init(document.getElementById('echart3'));

// 指定图表的配置项和数据
option = {
	title: {
		text: '本年度考勤'
	},
	tooltip: {
		trigger: 'axis'
	},
	legend: {
		data: ['应到天数', '实到天数']
	},
	grid: {
		left: '3%',
		right: '4%',
		bottom: '3%',
		containLabel: true
	},
	toolbox: {
		feature: {
			saveAsImage: {}
		}
	},
	xAxis: {
		type: 'category',
		boundaryGap: false,
		data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月']
	},
	yAxis: {
		type: 'value'
	},
	series: [{
			name: '应到天数',
			type: 'line',
			stack: '总量',
			data: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31]
		},
		{
			name: '实到天数',
			type: 'line',
			stack: '总量',
			data: [28, 28, 29, 30, 29, 30, 31, 31, 29, 31]
		}
	]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
